package goleetcode

func minCapability(nums []int, k int) int {
	lower, upper := nums[0], nums[0]
	for _, x := range nums {
		lower = min(lower, x)
		upper = max(upper, x)
	}

	for lower <= upper {
		mid := (lower + upper) / 2
		count, visited := 0, false
		for _, x := range nums {
			if x <= mid && !visited {
				count, visited = count + 1, true
			}else {
				visited = false
			}
		}
		if count >= k {
			upper = mid - 1
		} else {
			lower = mid + 1
		}
	}

	return lower
}